//package cn.aitrox.ry.common.config;
//
//import com.alibaba.nacos.api.config.annotation.NacosValue;
//import com.fasterxml.jackson.annotation.JsonAutoDetect;
//import com.fasterxml.jackson.annotation.PropertyAccessor;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.data.redis.cache.RedisCacheConfiguration;
//import org.springframework.data.redis.cache.RedisCacheManager;
//import org.springframework.data.redis.cache.RedisCacheWriter;
//import org.springframework.data.redis.connection.RedisConnectionFactory;
//import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
//import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
//import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
//import org.springframework.data.redis.serializer.RedisSerializationContext;
//import redis.clients.jedis.JedisPoolConfig;
//
//import java.time.Duration;
//
//@Configuration
//@ConditionalOnProperty(prefix = "spring.redis", name = "host")
//public class RedisCacheConfig {
//
//    @NacosValue("${spring.redis.host}")
//    private String host;
//
//    @NacosValue("${spring.redis.port}")
//    private int port;
//
//    /**
//     * jedis连接池配置
//     *
//     * @return
//     */
//    @Bean
//    public JedisPoolConfig jedisPoolConfig() {
//        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
//        // 最大连接数
//        jedisPoolConfig.setMaxTotal(100);
//        // 获取连接最大等待毫秒数
//        jedisPoolConfig.setMaxWaitMillis(10000);
//        // 连接池最大存活连接数
//        jedisPoolConfig.setMaxIdle(100);
//        // 连接池最小存活连接数
//        jedisPoolConfig.setMinIdle(50);
//        return jedisPoolConfig;
//    }
//
//    /**
//     * 连接redis的工厂类
//     *
//     * @return
//     */
//    @Bean
//    public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
//        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
//        redisStandaloneConfiguration.setHostName(host);
//        redisStandaloneConfiguration.setPort(port);
//
//        JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jpcf = (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder) JedisClientConfiguration.builder();
//        jpcf.poolConfig(jedisPoolConfig);
//
//        JedisClientConfiguration jedisClientConfiguration = jpcf.build();
//
//        JedisConnectionFactory factory = new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration);
//        return factory;
//    }
//
//    /**
//     * 设置RedisCacheManager
//     * 使用cache注解管理redis缓存
//     *
//     * @return
//     */
//    @Bean
//    @ConditionalOnBean(RedisConnectionFactory.class)
//    public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
//
//        //初始化一个RedisCacheWriter输出流
//        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
//
//        //采用Jackson2JsonRedisSerializer序列化机制
//        Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
//        ObjectMapper objectMapper = new ObjectMapper();
//        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
//        serializer.setObjectMapper(objectMapper);
//
//        //创建一个RedisSerializationContext.SerializationPair给定的适配器pair
//        RedisSerializationContext.SerializationPair<Object> pair = RedisSerializationContext.SerializationPair.fromSerializer(serializer);
//
//        //创建CacheConfig，设置序列器和缓存时间
//        RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(pair).entryTtl(Duration.ofHours(12));
//
//        return new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
//
//    }
//
//    @Bean
//    @ConditionalOnBean(RedisConnectionFactory.class)
//    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
//        return new StringRedisTemplate(redisConnectionFactory);
//    }
//}
